package com.buba.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.R;
import entity.salary.Payroll;
import entity.salary.dto.socialDto;
import entity.salary.vo.selPeopleSalaryInformationVo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.buba.service.PayrollService;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.PageResult;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author beisheng
 * @since 2023-03-14
 */
@RestController
@RequestMapping("/payroll")
public class PayrollController {

    @Resource
    private PayrollService payrollService;

    /* *
     * @Author: beisheng
     * @Descriprion: 分页查询所有工资单
     * @Date: 2023/3/23 13:36
     * @Return: common.R
     * @Param: []
     */

    @GetMapping("/selAllPayroll")
    public R selAllPayroll(@RequestBody socialDto socialDto){
        PageResult pageResult = payrollService.selAllPayroll(socialDto);
        return R.success(pageResult);
    }

    /* *
     * @Author: beisheng
     * @Descriprion:查看所有工资单关联人员的薪资信息
     * @Date: 2023/3/23 16:13
     * @Return: common.R
     * @Param: [payRollId]
     */

    @GetMapping("/selPeopleSalaryInformation/{payRollId}")
    public R selPeopleSalaryInformation(@PathVariable("payRollId") Integer payRollId){
        List<selPeopleSalaryInformationVo> salaryInformationVos = payrollService.selPeopleSalaryInformation(payRollId);
        return R.success(salaryInformationVos);
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 新建工资单
     * @Date: 2023/3/23 15:33
     * @Return: common.R
     * @Param: [payroll]
     */

    @PostMapping("/insertPayroll")
    public R insertPayroll(@RequestBody Payroll payroll){
        payroll.setCreateTime(new Date());
        payroll.setUpdateTime(new Date());
        payroll.setDeleteFlag(0);
        payrollService.save(payroll);
        return R.success();
    }
    /**
     * 功能描述: 新增或更新
     *
     * @param payroll
     * @return
     * @throws
     * @Author: zl
     */
    @PostMapping("/save")
    public R save(@RequestBody Payroll payroll) {
        payrollService.saveOrUpdate(payroll);
        return R.success();
    }

    /**
     * 功能描述: 删除
     *
     * @param id
     * @return
     * @throws
     * @Author: zl
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        payrollService.removeById(id);
        return R.success();
    }

    /**
     * 功能描述: 批量删除
     *
     * @param ids
     * @return
     * @throws
     * @Author: zl
     */
    @PostMapping("/deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        payrollService.removeByIds(ids);
        return R.success();
    }

    /**
     * 功能描述: 全查
     *
     * @param
     * @return
     * @throws
     * @Author: zl
     */
    @GetMapping("/findAll")
    public R findAll() {
        return R.success(payrollService.list());
    }

    /**
     * 功能描述: 单查
     *
     * @param id
     * @return
     * @throws
     * @Author: zl
     */
    @GetMapping("/findOne/{id}")
    public R findOne(@PathVariable Integer id) {
        return R.success(payrollService.getById(id));
    }

    /**
     * 功能描述: 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws
     * @Author: zl
     * @Date:   2022/7/28 19:15
     */
    @GetMapping("/findPage")
    public R findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize) {
        QueryWrapper<Payroll> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return R.success(payrollService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}
